package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService{
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMapper.selectCountByPhone(phone);
        return count > 0;
    }

    @Override
    public void sendVerifyCode(String phone) {


        //创建验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "")
                .substring(0, 4);

        //将验证码添加到redis中
        String key = RedisKeys.VERIFY_CODE.join(phone);

        //添加到redis库中
        userInfoRedisService.setVerifyCode(key,code,RedisKeys.VERIFY_CODE.getTime());

        //发送的短信
        System.out.println("您注册的短信验证码是: " + code + "请您在" + Consts.VERIFY_CODE_VAI_TIME + " 分钟内使用");
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode)throws LogicException {
        //判断参数是否为空
        AssertUtil.hasLength(phone,"手机号码不能为空");
        AssertUtil.hasLength(nickname,"名称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");

        //判断输入的两次密码是否一致
        AssertUtil.isEquals(password,rpassword,"两次输入的密码不一致");

        //判断手机号码格式是否正确

        //判断手机号码的唯一性
        if(this.checkPhone(phone)){
            throw new LogicException("手机号码已经被注册");
        }

        //判断验证码是否正确
        String code = userInfoRedisService.getVerifyCode(RedisKeys.VERIFY_CODE.join(phone));

        if(code == null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码失效或者错误");
        }

        //完成注册
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setNickname(nickname);
        userInfo.setPassword(password);
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setState(1);
        userInfo.setLevel(1);
        userInfoMapper.insert(userInfo);

    }

    @Override
    public String userLogin(String username, String password) throws LogicException {
        UserInfo user = userInfoMapper.selectByUsernameAndPassword(username,password);

        //查询用户是否存在
        if(user == null){
            //不存在的话,抛出异常
            throw new LogicException("账号或密码错误");
        }

        //创建token,然后存在redis里面去
        String token = UUID.randomUUID().toString().replaceAll("-", "");


        //将token作为redis中的key,value是登录对象存到redis缓存数据库中,并设置过时时间
        userInfoRedisService.addToken(token,user);

        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
